﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using schedulelist.ORM.dao.interfaces;
using schedulelist.ORM.domain;
using NHibernate;
namespace schedulelist.ORM.dao.classes
{
    class RoleDAO : GenericDAO<Role>, IRoleDAO
    {
        public RoleDAO(ISession session) : base(session) { }

        public Role getRoleByName(
        string name)
        {
            var list = session.CreateSQLQuery(
            "SELECT role.* FROM role" +
            " WHERE role.name='" + name + "'")
            .AddEntity("Role", typeof(Role))
            .List<Role>();
            Role r = list[0];
            return r;
        }

        public Role getRoleById(
        int id)
        {
            var list = session.CreateSQLQuery(
            "SELECT role.* FROM role" +
            " WHERE role.Id='" + id + "'")
            .AddEntity("Role", typeof(Role))
            .List<Role>();
            Role r = list[0];
            return r;
        }
        public Role getRoleByUserLogin(
        string log)
        {
            var list = session.CreateSQLQuery(
            "SELECT role.* FROM role WHERE role.Id IN "+
            "(SELECT user.RoleId FROM user WHERE user.login = '"+log+"')")
            .AddEntity("Role", typeof(Role))
            .List<Role>();
            Role r = list[0];
            return r;
        }
    }
}
